import { DateTime } from 'luxon'
import {column, BaseModel, beforeCreate, BelongsTo, hasMany, HasMany} from '@ioc:Adonis/Lucid/Orm'
import Spus from "App/Models/Spus";
import {belongsTo} from "@adonisjs/lucid/build/src/Orm/Decorators";
import uuid from 'uuid-v4'
import Param from "App/Models/Param";
import Order from "App/Models/Order";
export default class Skus extends BaseModel {
    @hasMany(() => Param, {
        foreignKey: 'skuId'
    })
    public params: HasMany<typeof Param>
    public static table = 'tb_skus'
    public static selfAssignPrimaryKey = true
    @column({ isPrimary: true })
    public id: string;
    @column()
    public spuId: string
    @column()
    public name: string
    @column()
    public images: string
    @column()
    public price: number
    @column()
    public stock: number
    @column()
    public paramKey: string;
    @column()
    public paramValue: string;
    @column.dateTime({ autoCreate: true })
    public createdAt: DateTime;
    @column.dateTime({ autoCreate: true, autoUpdate: true })
    public updatedAt: DateTime;
    @belongsTo(() => Spus)
    public spu: BelongsTo<typeof Spus>
    /**
     * Sku属于某个订单
     */
    @belongsTo(() => Order)
    public order: BelongsTo<typeof Order>
    @beforeCreate()
    public static assignUuid(sku: Skus) {
      sku.id = uuid()
    }
}
